package com.mrd.gtimpframe.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrd.base.Jqgrid;
import com.mrd.gtimpframe.entity.CustomCompanyInfo;
import com.mrd.gtimpframe.repository.CusInfoMapper;
import com.mrd.gtimpframe.service.CusInfoService;
import com.mrd.util.FormBeanUtil;
import com.mrd.util.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.data.domain.Page;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 查询公司信息Controller
 * @author han
 */
@Controller
@RequestMapping("/getCusInfo")
public class GetCusInfoController {

    @Autowired
    private CusInfoService cusInfoService;

    @RequestMapping("cusInfoList")
    public String listPage() {
        return "/gtimpframe/orderFor/getCusInfo";
    }

    @RequestMapping("findCusInfoDataList")
    @ResponseBody
    public Page findCusInfoDataList(Jqgrid jqgrid) {
        // List<CustomCompanyInfo> superCompanyInfos;
        try {
         /*   String comInfoId = LoginUtil.getLoginUser().getCompany().getId();
            CustomCompanyInfo params = FormBeanUtil.getEntityFromJqgrid(jqgrid, CustomCompanyInfo.class);
            Pageable page = jqgrid.getPage();
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
            Sort.Order order = jqgrid.getOrder();
            if (order != null) {
                String direction = order.getDirection().toString();
                String propertyName = order.getProperty();
                PageHelper.orderBy(propertyName + " " + direction);
            }
            //String comInfoId=params.getComInfoId();
            if(comInfoId!=null){
                params.setComInfoId(comInfoId);
            }
            if (!"".equals(params.getCtimeEnd())&&params.getCtimeEnd()!=null){
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                params.setCtimeEnd(sdf.format(sdf.parse(params.getCtimeEnd()).getTime()+60*60*24*1000));
            }
            superCompanyInfos = cusInfoService.selectSelective(jqgrid);
            List<Map<String,Object>> newData=new ArrayList<>();
            for (int i=0;i<superCompanyInfos.size();i++){
                if (superCompanyInfos.get(i).getComInfoId()!=comInfoId) {
                    Map<String, Object> map = new HashMap<>();
                    CustomCompanyInfo superCompanyInfo = superCompanyInfos.get(i);
                    map.put("comInfoId", superCompanyInfo.getComInfoId());
                    map.put("name", superCompanyInfo.getName());
                    map.put("uniteCode", superCompanyInfo.getUniteCode());
                    map.put("customsCode", superCompanyInfo.getCustomsCode());
                    map.put("tGenComContact", "联系人");
                    map.put("creator", superCompanyInfo.getCreator());
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String ctimeStr = formatter.format(superCompanyInfo.getCtime().getTime());
                    map.put("ctime", ctimeStr);
                    map.put("comvalidate", superCompanyInfo.getComValidate());
                    map.put("agentRefId", superCompanyInfo.getAgentRefId());
                    newData.add(map);
                }
            }
            PageInfo<Map<String,Object>> pi = new PageInfo<>(newData);
            return new PageImpl(newData, page, pi.getTotal());*/

            return cusInfoService.selectSelective(jqgrid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

    /**
     * 根据公司信息id删除委托关系
     * @param comInfoId
     * @return
     */
    @RequestMapping("/deleteCusInfo")
    @ResponseBody
    public String deleteCusInfo(String comInfoId) {
        try{
            if (comInfoId!=null){
                if (cusInfoService.deleteByPrimaryKey(comInfoId)>0)
                    return "删除成功";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  "删除失败";
    }

    /**
     * 根据公司id获取公司信息名列表
     * @return
     */
   /* @RequestMapping("/getName")
    @ResponseBody
    public Map<Object,Object> deleteGoods() {
        Map<Object,Object> result=null;
        try{
            String comInfoId = LoginUtil.getLoginUser().getCompany().getId();
            result=new HashMap<>();
            List<CustomCompanyInfo> CustomCompanyInfos = cusInfoMapper.selectSelectiveCompId(comInfoId);
            String[][] results = new String[CustomCompanyInfos.size()][1];
           *//* for (int i=0;i< results.length;i++){
                String name = CustomCompanyInfos.get(i);
                if (!"".equals(name)&&name!=null)
                results[i][0]=name;
            }*//*
            result.put("content",results);
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }*/
}
